package com.sourcetrip.raw.model;

/**
 * @author: ZhouBert
 * @date: 2020/12/9
 * @description: 可以继承二叉树的节点，用 K 作为节点的 key ,然后在 HashNode 中保存一个 value
 */
public class HashNode<K, V> extends MyBinaryNode<K> {

	private int color;

	private V value;


	/**
	 * constructor
	 *
	 * @param element
	 * @param parent
	 */
	public HashNode(K element, MyBinaryNode<K> parent) {
		super(element, parent);
	}

	public HashNode(K element, MyBinaryNode<K> parent, V value) {
		super(element, parent);
		this.value = value;
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public V getValue() {
		return value;
	}

	public void setValue(V value) {
		this.value = value;
	}

	/**
	 * 获取本节点的兄弟节点
	 *
	 * @return
	 */
	public HashNode<K, V> sibling() {
		if (isLeftChild()) {
			return (HashNode<K, V>) parent.right;
		}
		if (isRightChild()) {
			return (HashNode<K, V>) parent.left;
		}
		return null;
	}

	/**
	 * 判断本节点是否红色
	 * @return
	 */
	public boolean isRed() {
		return color == Color.RED;
	}

	/**
	 * 判断本节点是否黑色
	 * @return
	 */
	public boolean isBlack() {
		return color == Color.BLACK;
	}


	@Override
	public String toString() {
		return this.element.toString() + "(" + (color == Color.BLACK ? "B" : "R") + ")";
	}
}
